package com.bulletelc.apollopre.manager.imageload.glideloader;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bulletelc.apollopre.manager.imageload.IImageLoaderStrategy;
import com.bulletelc.apollopre.manager.imageload.ImageLoadProgressListener;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderOptions;

/**
 * author: lhl
 * date  : 2017/11/22
 * desc  : 基于Glide图片库的相应策略的封装
 */

public class GlideImageLoaderStrategy implements IImageLoaderStrategy {
    private Handler mainHandler=new Handler();

    @Override
    public void loadImage(@NonNull ImageLoaderOptions options) {
        GlideApp.with(options.getViewContainer().getContext())
                .load(options.getUrl())
                .into((ImageView) options.getViewContainer());
    }

    @Override
    public void loadImage(Context context, @NonNull ImageLoaderOptions options) {
        GlideApp.with(context)
                .load(options.getUrl())
                .into((ImageView) options.getViewContainer());
    }

    @Override
    public void loadImageWithProgress(String url, ImageView imageView, ImageLoadProgressListener listener) {

    }

    @Override
    public void pause(Context context) {
        GlideApp.with(context).pauseRequests();
    }

    @Override
    public void resume(Context context) {
        GlideApp.with(context).resumeRequests();
    }

    @Override
    public void clearDiskCache(final Context context) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                GlideApp.get(context).clearDiskCache();
                return null;
            }
        };
    }

    @Override
    public void clearMemoryCache(Context context) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
           GlideApp.get(context).clearMemory();
        }
    }

    @Override
    public void clearAllCache(Context context) {
        clearMemoryCache(context);
        clearDiskCache(context);
    }
}
